<?php

require_once '../application/modules/admin/controllers/SuperControllerAdmin.php';

class Admin_CommentsController extends Admin_SuperController {

    public function indexAction() {
        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->layout->disableLayout();
            if ($this->_request->event == 'removeRow')
                $this->onRemoweRow($this->_request->id);
            if ($this->_request->event == 'updateComm')
                $this->onUpdateComm($this->getRequest()->getParams());
        }

        $this->view->order_field = isset($this->_request->order_field) ? $this->_request->order_field : 'id';
        $this->view->order_dir = isset($this->_request->order_dir) ? $this->_request->order_dir : 'DESC';

        $this->view->tableColumn = array(
            0 => array(
                'title' => 'page_id',
                'field' => 'page_id'
            ),
            1 => array(
                'title' => 'name',
            ),
            2 => array(
                'title' => 'comment',
                'wrap' => 'div300'
            ),
            5 => array(
                'title' => 'date_create',
                'field' => 'date_create'
            ),
            6 => array(
                'title' => 'Change',
            ),
        );

        $view_page = new Zend_Session_Namespace('view_apage');
        $onPage = $view_page->on_page;
        $page = (int) $this->_request->page;
        if (empty($page))
            $page = 1;

        $qAr = array(
            'table_name' => 'comments',
            'order_field' => $this->view->order_field,
            'pages' => $this->_request->pages,
            'order_dir' => $this->view->order_dir,
            'offset' => ($page - 1) * $onPage,
            'limit' => $onPage,
        );
        $CONTM = new Admin_Model_Content();
        $this->view->info = $CONTM->getItems($qAr, $count_rows);
        $this->view->countRow = $count_rows;
        if (is_array($this->view->info) && count($this->view->info)) {
            $PGM = new Application_Model_Pages();
            foreach ($this->view->info as &$val) {
                $page = $PGM->getPageByID($val['page_id']);
                $val['url'] = $PGM->getPageUrl($val['page_id']);
                $val['pname'] = $page['title'];
            }
        }
        //_debug($this->view->info);

        $paginator = Zend_Paginator::factory((int) $this->view->countRow);
        $paginator->setDefaultItemCountPerPage($onPage);
        $paginator->setCurrentPageNumber($page);

        $this->view->paginator = $paginator;
        $this->view->onPage = $onPage;
        $this->view->find = $qAr;
    }

    protected $sructure = array(
        0 => array('name' => 'name', 'type' => 'str', 'valid' => true,),
        3 => array('name' => 'comment', 'type' => 'text', 'style' => "width: 250px; height: 140px;", 'valid' => true,),
    );

    public function editLabelsAction() {
        Zend_Layout::getMvcInstance()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $cat_id = (int) $this->_request->id;
        if (!empty($cat_id)) {
            $CONTM = new Admin_Model_Content();
            $info = $CONTM->getOneObj('comments', $cat_id);
            if (is_array($info)) {
                $this->info = array();
                $this->info['id'] = $info['id'];
                $this->info['event'] = 'updateComm';
                $this->info['fields'] = $this->addValue($this->sructure, $info);
                include (APPLICATION_PATH . '/modules/admin/views/scripts/snippets/lego.phtml');
            }
        } else {
            throw new Exception('no parameters required');
        }
    }

    protected function onUpdateComm($data) {
        if (!empty($data)) {
            $ACNTM = new Admin_Model_Content();
            $ACNTM->update('comments', $data);
        }
    }

    protected function onRemoweRow($id) {
        if (!empty($id)) {
            $ACNTM = new Admin_Model_Content();
            $ACNTM->removeRow('comments', $id);
        }
    }

}
